package com.cunshe.web.manage;

import com.cunshe.common.ResponseVO;
import com.cunshe.config.annotation.Login;
import com.cunshe.dao.SeriesDao;
import com.cunshe.domain.Series;
import com.cunshe.entity.vo.SeriesInsertVO;
import com.cunshe.entity.vo.SeriesUpdateVO;
import com.cunshe.service.SeriesService;
import com.cunshe.web.BaseController;
import com.sunkee.commons.utils.ConvertUtil;
import com.sunkee.commons.utils.ListUtils;
import com.sunkee.commons.utils.bean.List2Tree;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 产品系列
 * @author xiao
 */
@Api(tags = "产品系列")
@Login
@RestController
@RequestMapping(value = "/manage/series")
public class SeriesController extends BaseController {

    @Autowired
    private SeriesService seriesService;

    /** 新增 */
    @ApiOperation(value = "新增")
    @PostMapping(value = "insert")
    public ResponseVO insert(@RequestBody @Validated SeriesInsertVO vo) {
        Series record = ConvertUtil.copyProperties(vo, Series.class);
        record.setCreateBy(getCurrentUser().getUsername());
        seriesService.insert(record);
        return ResponseVO.success();
    }

    /** 删除 */
    @ApiOperation(value = "删除")
    @PostMapping(value = "delete")
    public ResponseVO delete(@RequestParam("id") Long id) {
        seriesService.delete(id);
        return ResponseVO.success();
    }

    /** 修改 */
    @ApiOperation(value = "修改")
    @PostMapping(value = "update")
    public ResponseVO update(@RequestBody @Validated SeriesUpdateVO vo) {
        Series record = ConvertUtil.copyProperties(vo, Series.class);
        record.setUpdateBy(getCurrentUser().getUsername());
        seriesService.update(record);
        return ResponseVO.success();
    }

    /** 查询系列 */
    @ApiOperation(value = "查询系列")
    @PostMapping(value = "tree")
    public ResponseVO tree(Series record) {
        List<Series> list = seriesService.list(record);
        list = (List<Series>) ListUtils.list2Tree(list);
        return ResponseVO.success(list);
    }

}